package anti5mao;

import java.util.Date;


import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;

public class Setting {
	/*public static Setting get(String name) {
		Entity entity = new Entity("Setting", "name");
		entity.setProperty("name", name);
		DatastoreService datastore = DatastoreServiceFactory
				.getDatastoreService();

		try {
			entity = datastore.get(entity.getKey());
		} catch (EntityNotFoundException e) {
			entity.setProperty("last", new Date());
			e.printStackTrace();
		}

//		entity.setProperty("access_token", accesstoken);
//		entity.setProperty("access_token_secret", accessTokenSecret);
		datastore.put(entity);
		return new Setting(entity);
	}*/
	
	
	public static void insert(String name,String accessToken,String accessTokenSecret){
		DatastoreService datastore = DatastoreServiceFactory
		.getDatastoreService();
		Entity entity = new Entity("Setting", "name");
		entity.setProperty("name", name);
		entity.setProperty("last", new Date());
		entity.setProperty("access_token", accessToken);
		entity.setProperty("access_token_secret", accessTokenSecret);
		datastore.put(entity);
	}

	
	private String name;
	private Date last;//last excute time
	private String accessToken;
	private String accessTokenSecret;
	
	private Setting(Entity data) {
		this.name = (String) data.getProperty("name");
		this.last = (Date) data.getProperty("last");
		this.accessToken = (String) data.getProperty("access_token");
		this.accessTokenSecret = (String) data.getProperty("access_token_secret");
	}
	
	public void save() {
		Entity entity = new Entity("Setting", "name");
		
		DatastoreService datastore = DatastoreServiceFactory
				.getDatastoreService();
		entity.setProperty("name", name);
		entity.setProperty("last", last);
		entity.setProperty("access_token", accessToken);
		entity.setProperty("access_token_secret", accessTokenSecret);
		datastore.put(entity);
	}
	
	
	
	public String getAccessToken() {
		return accessToken;
	}

	public void setAccessToken(String accessToken) {
		this.accessToken = accessToken;
	}

	public String getAccessTokenSecret() {
		return accessTokenSecret;
	}

	public void setAccessTokenSecret(String accessTokenSecret) {
		this.accessTokenSecret = accessTokenSecret;
	}

	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Date getLast() {
		return last;
	}
	public void setLast(Date last) {
		this.last = last;
	}

	
}
